package com.zx.mobile.controller;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zx.map.dao.model.MessageInfo;
import com.zx.map.service.MessageInfoService;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.utils.WebUtils;

@Controller
@RequestMapping("messageInfo")
public class MessageInfoController {

	@Autowired
	private MessageInfoService messageInfoService;
	
	@RequestMapping("/sendMessage")
	@ResponseBody
	public Map sendMessage(@RequestBody MessageInfo messageInfo, HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			messageInfo.setMerchantId(merchantId);
			messageInfo.setSendDatetime(new Date());
			messageInfoService.sendMessage(messageInfo);
			return CommonResponseUtils.successResponse();
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
	
	@RequestMapping("msgList")
	@ResponseBody
	public Map msgList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
		
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			
			PageInfo<MessageInfo> pageInfo = messageInfoService.msgList(merchantId, Integer.parseInt((String) params.get("pageNum")), Integer.parseInt((String)params.get("pageSize")));
			return CommonResponseUtils.successResponse(pageInfo.getList());
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
		
	}
}
